package com.lifeng.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import redis.clients.jedis.JedisPoolConfig;
/**
 * @Description: 原来在spring中实例化的bean,
 * 这里都要实例化
 * @author  lifeng
 * @date 2017年5月10日 下午10:49:23 
 * @version V1.0
 */
@Configuration
public class RedisConfiguration {
	@Autowired
	private RedisProp redisProp;
	/**Jedis连接池*/
	@Bean(name= "jedisPoolConfig") 
    public JedisPoolConfig jedisPool() {  
		JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
		//设置连接池相关的属性
		jedisPoolConfig.setMaxIdle(redisProp.getMaxIdle());
		jedisPoolConfig.setMaxTotal(redisProp.getMaxTotal());
        return jedisPoolConfig;  
    }  
	/**Jedis连接工厂*/
	@Bean(name= "jedisConnectionFactory")  
    public JedisConnectionFactory gedisConnectionFactory(JedisPoolConfig jedisPoolConfig) {  
		JedisConnectionFactory factory = new JedisConnectionFactory(jedisPoolConfig);
		factory.setDatabase(redisProp.getDatabase());
		factory.setHostName(redisProp.getHost());
		factory.setPort(redisProp.getPort());
		return factory;  
    } 
	/**redisTemplate*/
	@Bean(name= "redisTemplate")  
    public RedisTemplate redisTemplate(JedisConnectionFactory jedisConnectionFactory) {  
		RedisTemplate redisTemplate = new RedisTemplate();
		redisTemplate.setConnectionFactory(jedisConnectionFactory);
		//设置键的序列化
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		//设置值得序列化
		redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
		System.out.println("=========RedisTemplate 实例化成功================");
		return redisTemplate;  
    } 
}
